<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer extends MY_Controller {

	function __construct() {
		parent::__construct();
	}

	function add_new_passengers() {
		$this->load->model('u_customer_passenger', 'customer_passenger_m');
		
		$passengers_data = $this->input->post('passengers_data');
		
		foreach ($passengers_data as $k => $passenger_data) {
			$passenger_data['user_id'] = $_SESSION['uid'];
			$new_passenger_ids[$k] = $this->customer_passenger_m->save($passenger_data);
		}
		
		/*echo '<pre>';
		print_r($passenger_data);
		return ;*/
		
		echo json_encode($new_passenger_ids);
	}
	
	function get_passengers() {
		$this->load->model('u_customer_passenger', 'customer_passenger_m');
		$this->load->model('o_order', 'order_m');
		$this->load->model('f_flight', 'flight_m');
		$this->load->model('link_order_passenger', 'order_passenger_m');
		
		$data['passengers'] = $this->customer_passenger_m->search_by_condition(array('user_id' => $_SESSION['uid']));
		$orders = $this->order_m->search_by_condition(array('user_id' => $_SESSION['uid']));
		
		foreach ($orders as $order) {
			$temp = $this->flight_m->get_flight_data_by_id($order['flight_id']);
			
			if (strtotime($temp['departure_date'] . ' ' . $temp['departure_time']) > time()) {
				//echo $temp['departure_date'] . ' ' . $temp['departure_time'];
				$vaild_order_ids[] = $order['id'];
			}
		}
		
		$data['valid_order_passenger_ids'] = array();
		
		if (!empty($vaild_order_ids)) {
			foreach ($vaild_order_ids as $vaild_order_id) {
				$temp = $this->order_passenger_m->get_by('order_id', $vaild_order_id, 'passenger_id');
					
				foreach ($temp as $v) {
					$valid_order_passenger_ids[] = $v;
				}
			}
			
			$valid_order_passenger_ids = array_unique($valid_order_passenger_ids);
			$data['valid_order_passenger_ids'] = $valid_order_passenger_ids;
		}
		
		
		//var_dump($valid_order_passenger_ids);
		$this->load->view('module/index/my_page_passengers_block', $data);
	}
	
	function get_passenger() {
		$this->load->model('u_customer_passenger', 'customer_passenger_m');
		
		$id = $this->input->post('id');
		$data['passenger'] = $this->customer_passenger_m->get_by_id($id);
		
		$this->load->view('module/index/my_page_passenger_edit_block', $data);
	}
	
	function save_passenger() {
		$this->load->model('u_customer_passenger', 'customer_passenger_m');
		
		$post_data = $this->input->post();
		
		
		if ($post_data['passenger_type'] == 'C') {
			$post_data['certification_type'] = '1';
		} elseif ($post_data['passenger_type'] == 'I') {
			$post_data['certification_type'] = '';
			$post_data['certification_number'] = '';
		}
		
		$update_data = $post_data;
		
		$this->customer_passenger_m->update($update_data);
		$this->output->set_output('succeed');
	}
	
	function delete_passenger() {
		$this->load->model('u_customer_passenger', 'customer_passenger_m');
		
		$id = $this->input->post('id');
		$this->customer_passenger_m->update(array('id' => $id, 'flag' => IS_DELETE));
		
		$this->output->set_output('succeed');
	}
	
	function get_orders() {
		$this->load->model('o_order', 'order_m');
		
		$orders = $this->order_m->search_by_condition(array('user_id' => $_SESSION['uid'], 'flag' => ''));
		
		foreach ($orders as $k => $order) {
			$orders[$k] = $this->get_order_detail($order['id'], TRUE);
		}
		
		//echo '<pre>';
		//print_r($orders);
		
		$this->load->view('module/index/my_reservation_block', array('orders' => $orders));
	}
	
	function get_order_detail($order_id, $return_array = FALSE) {
		$this->load->model('o_order', 'order_m');
		$this->load->model('link_order_passenger', 'order_passenger_m');
		$this->load->model('u_customer_passenger', 'customer_passenger_m');
		$this->load->model('f_flight', 'flight_m');
		$this->load->model('const_airport', 'airport_m');
		$this->load->model('const_area', 'area_m');
		
		$order = $this->order_m->get_by_id($order_id);
		
		$passenger_ids = $this->order_passenger_m->get_by('order_id', $order['id'], 'passenger_id');
		foreach ($passenger_ids as $passenger_id) {
			$order['passenger'][] = $this->customer_passenger_m->get_by_id($passenger_id);
		}
		
		$order['flight'] = $this->flight_m->get_flight_data_by_id($order['flight_id']);
		$order['flight']['from']['airport'] = $this->airport_m->get_by_id($order['flight']['from_airport_id']);
		$order['flight']['from']['area'] = $this->area_m->get_by_id($order['flight']['from']['airport']['area_id']);
		$order['flight']['to']['airport'] = $this->airport_m->get_by_id($order['flight']['to_airport_id']);
		$order['flight']['to']['area'] = $this->area_m->get_by_id($order['flight']['to']['airport']['area_id']);
		
		/*echo '<pre>';
		print_r($order);*/
		
		if ($return_array == TRUE) {
			return $order;
		} else {
			$this->load->view('module/index/my_reservation_detail_block', array('order' => $order));
		}
	}
	
	function delete_order($order_id) {
		$this->load->model('o_order', 'order_m');
		$this->load->model('f_flight', 'flight_m');
		
		$order = $this->get_order_detail($order_id, TRUE);
		if ($order['user_id'] == $_SESSION['uid']) {
			$this->order_m->update(array('id' => $order_id, 'flag' => IS_DELETE));
			
			if ($order['class'] == 'Y') {
				$cabin_class_name = 'economy_class';
			} else {
				$cabin_class_name = 'prestige_class';
			}
			
			$flight = $this->flight_m->get_by_id($order['flight_id']);
			$this->flight_m->update(array('id' => $order['flight_id'], $cabin_class_name . '_remain_seats' => ($flight[$cabin_class_name . '_remain_seats'] + count($order['passenger']))));
			
			$this->output->set_output('succeed');
		}
	}
}